<template>
  <div>
    <el-row style="margin-bottom: 10px">
      <el-col>
        <div :id="id" :class="className" :style="{height:height,width:width}" />
      </el-col>
    </el-row>
  </div>
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
export default {
  mixins: [resize],
  props: {
    id: {
      type: String,
      default: 'line'
    },
    className: {
      type: String,
      default: 'line'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '400px'
    },
    lineChartData: {
      type: Object,
      default: Object
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    const that = this
    this.chart = echarts.init(document.getElementById(this.id))

    window.onresize = function() {
      that.chart.resize()
    }

    this.$nextTick(() => {
      this.initChart()
      this.setOptions(this.lineChartData)
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart.setOption(this.lineChartData)
    },
    setOptions({ expectedData, actualData } = {}) {
      this.chart.setOption({
        grid: { // 控制位置
          top: '15%',
          left: '5%',
          right: '5%',
          bottom: '10%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: actualData
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            data: expectedData,
            type: 'line',
            smooth: true,
            areaStyle: {
              color: 'rgba(0,135,254,0.3)'
            },
            color: [ // 柱子颜色
              '#0087FE'
            ],
            label: {
              show: true,
              position: 'top',
              color: '#C8C8C8'
            }
          }

        ]
      })
    }
  }
}
</script>
